/*
 * $Id: PropertiesDAO.java,v 1.3 2009/07/10 15:18:35 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.List;
import java.util.Properties;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.CfgProperties;

/**
 * @author 212042764
 * 
 */
public class PropertiesDAO extends BaseDAO implements IPropertiesDAO {
	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = -8900337005396103372L;
	private static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.CfgProperties";
	private static final String ALL = "ALL";
	private static final String COL_STEP_TYPE = "stepType";

	public PropertiesDAO() {
		super(TABLE_NAME);
	}

	public void save(Object pojo) {
		super.save(pojo);
	}

	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

	public Properties getForStepType(String stepTypeName) {
		if (ASCUtil.isNullOrEmpty(stepTypeName)) {
			return null;
		}
		StringBuffer queryString = new StringBuffer().append("from ").append(
				TABLE_NAME).append(" where upper(").append(COL_STEP_TYPE).append(
				") = ? or upper(").append(COL_STEP_TYPE).append(") = ?");
		List<?> rtvdProperties = getHibernateTemplate().find(
				queryString.toString(),
				new Object[] { stepTypeName.toUpperCase(), ALL.toUpperCase() });

		Properties dbProperties = null;
		if (!ASCUtil.isNullOrEmptyList(rtvdProperties)) {
			dbProperties = new Properties();
			for (int iCount = 0; iCount < rtvdProperties.size(); iCount++) {
				CfgProperties property = (CfgProperties) rtvdProperties
						.get(iCount);
				if (ALL.equalsIgnoreCase(property.getStepType())) {
					if (ASCUtil.isNullOrEmpty(dbProperties.getProperty(property
							.getPropertyName().trim()))) {
						dbProperties.put(property.getPropertyName().trim(), property
								.getPropertyValue().trim());
					}
				} else {
					dbProperties.put(property.getPropertyName().trim(), property
							.getPropertyValue().trim());
				}
			}
		}
		return dbProperties;
	}

}
